package com.rg.inf.action;

import com.rd.sys.dto.client.sync.MsgBody;
import com.rd.sys.dto.client.sync.ResultSyncMsgBody;
import com.rd.sys.dto.commmon.base.EyeException;
import com.rd.sys.dto.commmon.base.EyeResult;
import com.rd.sys.dto.sys.user.UserInfoDto;
import com.rd.sys.dto.sys.user.UserSubscriptionDto;
import com.rd.sys.service.analysis.result.ResultService;

public class ResultSyncAction extends BaseExecuteAction {
    private ResultService resultService = (ResultService) getService("resultServiceImpl");

    @Override
    public EyeResult execute(UserInfoDto userInfo, MsgBody msgBody, Object... objs) throws EyeException, Exception {
        ResultSyncMsgBody body = (ResultSyncMsgBody) msgBody;

        /** 1.检查项目存在性 */
        super.checkProjectNotExist(userInfo.getUsercode(), body.getProjectCode());

        /** 2.检查方案存在性 */
        super.checkSchemeNotExist(userInfo.getUsercode(), body.getProjectCode(), body.getSchemeCode());

        /** 3.检查结果存在性 */
        boolean isExist = super.checkResultExist(userInfo.getUsercode(), body.getProjectCode(), body.getSchemeCode(),
                body.getResultCode(), false); // 存在则更新,将不抛出异常

        /** 4.检查订购关系是否正常 */
        UserSubscriptionDto sub = super.checkSubscription(userInfo.getUserno());

        if (isExist) {
            /** 5.若结果已存在,则更新结果信息 */
            resultService.syncResult(userInfo.getUserno(), userInfo.getUsercode(), body.getProjectCode(),
                    body.getSchemeCode(), super.transResult(body), false);
        } else {
            /** 6.新增方案不存在,检查订购套餐是否正常 */
            super.checkPackage(sub, false, false, true);

            /** 7.同步新增的结果信息 */
            resultService.syncResult(userInfo.getUserno(), userInfo.getUsercode(), body.getProjectCode(),
                    body.getSchemeCode(), super.transResult(body), true);
        }

        return new EyeResult(EyeResult.ResultDefine.COMMON_SUCCESS);
    }
}
